package com.zj.study.file;

import lombok.experimental.Accessors;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>Title: FileTxtTest</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2024 版权</p>
 * <p>Company: Oceanpayment</p>
 *
 * @author Jerry Zhao
 * @version V1.0
 * @date 2024年07月24号 18点44分02秒
 */
public class FileTxtTest {


    private static final String DELIMITER = ",";

    public static void main(String[] args) {
        String filePath = "C:\\Users\\jerryzhao\\Desktop\\20240713000000-customerNegativeBalanceReport-XYZ_program_mgmt-Goldcard.txt";
        List<Data> dataList = new ArrayList<>();

        String fileName = "20240713000000-customerNegativeBalanceReport-XYZ_program_mgmt-Goldcard.txt";
        String substring = fileName.substring(0, 8);
        System.out.println("substring = " + substring);
        String program = fileName.substring(fileName.lastIndexOf("-") + 1, fileName.lastIndexOf("."));
        System.out.println("program = " + program);
//        try (BufferedReader ignored = new BufferedReader(new FileReader(filePath))) { // 保留try-with-resources确保资源释放
//            // 使用Files.lines结合流式处理，提高处理效率
//
//            dataList = Files.lines(Paths.get(filePath))
//                    .skip(1) // 跳过标题行
//                    .map(FileTxtTest::parseLine)
//                    .filter(Objects::nonNull) // 过滤掉解析失败的数据行
//                    .collect(Collectors.toList());
//        } catch (IOException e) {
//            System.err.println("文件读取失败: " + e.getMessage());
//            // 可以考虑记录日志或者向上抛出自定义异常
//        }
//
//        System.out.println("dataList = " + dataList);

//        try {
//            FileInputStream fs = new FileInputStream(filePath);
//            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
//            br.readLine();// 跳过第一行不做处理
//            String strTemp;
//            while ((strTemp = br.readLine()) != null) {
//                String[] values = strTemp.split(DELIMITER);
//                Data data = new Data();
//                data.setPortfolioId(values[1]);
//                data.setPortfolioName(values[2]);
//                data.setPortfolioType(values[3]);
//                data.setCurrency(values[4]);
//                data.setBalance(values[5]);
//                data.setComplianceBalance(values[6]);
//                dataList.add(data);
//            }
//
//            System.out.println("dataList = " + dataList);
//        }catch (Exception e) {
//            System.err.println("文件读取失败: " + e.getMessage());
//        }

    }

    private static Data parseLine(String line) {
        String[] columns = line.split(FileTxtTest.DELIMITER);
        if (columns.length < 7) {
            System.err.println("数据行不完整: " + line);
            return null; // 返回null以标记该行解析失败
        }

        try {
            Data data = new Data();
            data.setOperatingProgramId(columns[0])
                    .setPortfolioId(columns[1])
                    .setPortfolioName(columns[2])
                    .setPortfolioType(columns[3])
                    .setCurrency(columns[4])
                    .setBalance(columns[5])
                    .setComplianceBalance(columns[6]);
            return data;
        } catch (Exception e) {
            System.err.println("数据解析错误: " + line + ", 错误信息: " + e.getMessage());
            // 可以考虑记录日志或者向上抛出自定义异常
            return null;
        }
    }

    @lombok.Data
    @Accessors(chain = true)
    public static class Data {
        private String operatingProgramId;
        private String portfolioId;
        private String portfolioName;
        private String portfolioType;
        private String currency;
        private String balance;
        private String complianceBalance;
    }


}